package com.tgpms.web.hongbo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.interfaces.dto.HongBoInterface;
import com.tgpms.web.hongbo.dao.MaholeMapper;
import com.tgpms.web.hongbo.entity.Mahole;
import com.tgpms.web.hongbo.service.MaholeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created By Riven on 2020-9-15
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class MaholeServiceImpl implements MaholeService {

    @Resource
    private HongBoInterface hongBoInterface;

    @Resource
    private MaholeMapper maholeMapper;

    @Override
    public int syncMahole() throws UnsupportedEncodingException {
        int returnInt;
        List<Mahole> list = new ArrayList<>();
        int ifEmpty = maholeMapper.deleteMahole();
        if (ifEmpty != -1) {
            //  查询处理宏波数据批量插入
            Result tubeResult = hongBoInterface.getMahole(-1, -1, null);
            if (tubeResult.getSuccess()) {
                String resData = tubeResult.getData().toString();
                JSONObject resJson = JSONObject.parseObject(resData);
                JSONArray tables = (JSONArray) resJson.get("tables");
                for (Object table : tables) {
                    JSONObject jsonObject = JSONObject.parseObject(table.toString());
                    Mahole temp = transform(jsonObject, "D");
                    list.add(temp);
                }
                Result maholeCompleteResult = hongBoInterface.getMaholeComplete(-1, -1, null);
                String maholeCompleteResultData = maholeCompleteResult.getData().toString();
                JSONObject maholeCompleteResultJson = JSONObject.parseObject(maholeCompleteResultData);
                JSONArray maholeCompleteTables = (JSONArray) maholeCompleteResultJson.get("tables");
                for (int i = 0; i < maholeCompleteTables.size(); i++) {
                    JSONObject jsonObject = JSONObject.parseObject(tables.get(i).toString());
                    Mahole temp = transform(jsonObject, "C");
                    list.add(temp);
                }
            }
        }
        returnInt = maholeMapper.batchInsert(list);
        return returnInt;
    }

    private Mahole transform(JSONObject jsonObject, String type) {
        Mahole temp = new Mahole();
        temp.setId(jsonObject.get("ID").toString());
        temp.setCadCode(jsonObject.get("CADCode").toString());
        temp.setCode(jsonObject.get("Code").toString());
        temp.setAreaCode(jsonObject.get("AreaCode").toString());
        temp.setWorkArea(jsonObject.get("WorkArea").toString());
        temp.setAreaName(jsonObject.get("AreaName").toString());
        temp.setBidCap(jsonObject.get("BidCap").toString());
        temp.setCadMap(jsonObject.get("CADMap").toString());
        temp.setPosX(jsonObject.get("PosX").toString());
        temp.setPosY(jsonObject.get("PosY").toString());
        temp.setOptionType(jsonObject.get("OptionType").toString());
        temp.setRoadName(jsonObject.get("Road_Name").toString());
        temp.setNaturalHeight(jsonObject.get("NaturalHeight").toString());
        temp.setManholeType(jsonObject.get("Manhole_Type").toString());
        temp.setSubsidiary(jsonObject.get("Subsidiary").toString());
        temp.setManholeClass(jsonObject.get("Manhole_Class").toString());
        temp.setWellMaterial(jsonObject.get("Well_Material").toString());
        temp.setCoverMaterial(jsonObject.get("Cover_Material").toString());
        temp.setCoverShape(jsonObject.get("Cover_Shape").toString());
        temp.setSize1(jsonObject.get("Size").toString());
        temp.setSize2(jsonObject.get("Size2").toString());
        temp.setBuildYear(jsonObject.get("Build_Year").toString());
        temp.setOwner(jsonObject.get("Owner").toString());
        temp.setDesigner(jsonObject.get("Designer").toString());
        temp.setConstructer(jsonObject.get("Constructer").toString());
        temp.setSupervisor(jsonObject.get("Supervisor").toString());
        temp.setFillTime(jsonObject.get("Fill_Time").toString());
        temp.setFillPerson(jsonObject.get("Fill_Person").toString());
        temp.setMemo(jsonObject.get("Memo").toString());
        temp.setType(type);
        temp.setInsertUser("TGPMS");
        temp.setInsertDate(new Date());
        temp.setRowstamp(jsonObject.get("ID").toString());
        return temp;
    }
}
